package com.pandora.credit.web;

import com.pandora.common.parameter.annotation.RequestData;
import com.pandora.credit.dto.request.*;
import com.pandora.credit.dto.response.*;
import com.pandora.credit.mongo.service.MongoActivityService;
import com.pandora.credit.mongo.service.MongoBannerService;
import com.pandora.credit.mongo.service.MongoNewsService;
import com.pandora.credit.mongo.service.MongoPandoraCreditService;
import com.pandora.credit.service.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;

/**
 * @author developer
 */
@RestController
public class BusinessController {
    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private CreditService creditService;
    @Autowired
    private CustomerService customerService;
    @Autowired
    private MongoBannerService mongoBannerService;
    @Autowired
    private MongoNewsService mongoNewsService;
    @Autowired
    private MetaDataService metaDataService;
    @Autowired
    private MongoActivityService mongoActivityService;
    @Autowired
    private HistoryService historyService;
    @Autowired
    private AnnouncementService announcementService;
    @Autowired
    private MessageService messageService;
    @Autowired
    private ShareService shareService;
    @Autowired
    private MongoPandoraCreditService mongoPandoraCreditService;
    @Autowired
    private TrackService trackService;

    /**
     * 贷款分类列表
     */
    @RequestMapping(value = "loans/type", method = RequestMethod.GET)
    public LoanResponseNextLong doGetLoansType(
            @RequestHeader(value = "User-Agent", required = false, defaultValue = "") String ua) {
        return creditService.doGetLoansType(ua);
    }

    /**
     * 贷款列表
     */
    @RequestMapping(value = "loans", method = RequestMethod.GET)
    public LoanResponseNextInt doGetLoans(@RequestHeader(value = "User-Agent", required = false, defaultValue = "") String ua,
                                          @RequestData LoanRequest req) {
        return creditService.doGetLoans(req, ua);
    }

    /**
     * 用户点击过的贷款列表
     */
    @RequestMapping(value = "customers/{custId}/loans", method = RequestMethod.GET)
    public LoanResponseNextStr doCustomerLoans(
            @RequestHeader(value = "Authorization", required = false, defaultValue = "") String authorizationToken,
            @PathVariable String custId,
            @RequestData CustomerLoansRequest req) {
        return customerService.doCustomerLoans(req, authorizationToken);
    }

    /**
     * 征信列表
     */
    @RequestMapping(value = "credits", method = RequestMethod.GET)
    public LoanResponseNextLong doGetCredits(
            @RequestHeader(value = "access_token", required = false, defaultValue = "") String accessToken,
            @RequestHeader(value = "Authorization", required = false, defaultValue = "") String authorizationToken,
            @RequestData LoanRequest req) {
        return creditService.doGetCredits(req, authorizationToken, accessToken);
    }

    /**
     * 用户点击过的贷款列表
     */
    @RequestMapping(value = "customers/{custId}/credits", method = RequestMethod.GET)
    public LoanResponseNextNo doGetCustomerCredit(
            @RequestHeader(value = "Authorization", required = false, defaultValue = "") String authorizationToken,
            @PathVariable String custId) {
        return creditService.doGetCustomerCredit(custId, authorizationToken);
    }

    /**
     * 银行列表
     */
    @RequestMapping(value = "banks", method = RequestMethod.GET)
    public LoanResponseNextLong doGetBanks(@RequestData CreditCardRequest req) {
        return creditService.doGetBanks(req);
    }

    /**
     * 信用卡列表
     */
    @RequestMapping(value = "credit_cards", method = RequestMethod.GET)
    public LoanResponseNextInt doGetCreditCards(@RequestData CreditCardRequest req) {
        return creditService.doGetCreditCards(req);
    }

    /**
     * 用户点击过的信用卡列表
     */
    @RequestMapping(value = "customers/{custId}/credit_cards", method = RequestMethod.GET)
    public LoanResponseNextStr doCustomerCreditCards(
            @RequestHeader(value = "Authorization", required = false, defaultValue = "") String authorizationToken,
            @PathVariable String custId,
            @RequestData CustomerLoansRequest req) {
        return creditService.doCustomerCreditCards(authorizationToken, req);
    }

    /**
     * 获取banner
     */
    @RequestMapping(value = "banners", method = RequestMethod.GET)
    public LoanResponseNextNo doGetBanners(
            @RequestHeader(value = "User-Agent", required = false, defaultValue = "") String ua) {
        return mongoBannerService.doGetBanners(ua);
    }

    /**
     * 资讯
     */
    @RequestMapping(value = "news", method = RequestMethod.GET)
    public LoanResponseNextInt doGetNews(
            @RequestHeader(value = "User-Agent", required = false, defaultValue = "") String ua,
            @RequestData NewsRequest req) {
        return mongoNewsService.doGetNews(req, ua);
    }

    /**
     * 关于
     */
    @RequestMapping(value = "about", method = RequestMethod.GET)
    public AboutResponse doGetAbout() {
        return metaDataService.doGetAbout();
    }

    /**
     * 发现
     */
    @RequestMapping(value = "activities", method = RequestMethod.GET)
    public LoanResponseNextInt doGetActivities(@RequestData NewsRequest req) {
        return mongoActivityService.doGetActivities(req);
    }

    /**
     * 上传操作记录
     */
    @RequestMapping(value = "histories", method = RequestMethod.POST, consumes = {MediaType.APPLICATION_JSON_UTF8_VALUE})
    public Object doPostHistories(
            @RequestHeader(value = "Authorization", required = false, defaultValue = "") String authorizationToken,
            @RequestBody HistoriesRequest req,
            HttpServletResponse response) {
        Object ret = historyService.doPostHistories(authorizationToken, req);
        if (ret == null) {
            response.setStatus(204);
        } else {
            ret = new HistoriesResponse(String.valueOf(ret));
        }
        return ret;
    }
    @RequestMapping(value = "histories", method = RequestMethod.POST, consumes = {MediaType.APPLICATION_FORM_URLENCODED_VALUE})
    public Object doPostHistoriesForm(
            @RequestHeader(value = "Authorization", required = false, defaultValue = "") String authorizationToken,
            @RequestData HistoriesRequest req,
            HttpServletResponse response) {
        Object ret = historyService.doPostHistories(authorizationToken, req);
        if (ret == null) {
            response.setStatus(204);
        } else {
            ret = new HistoriesResponse(String.valueOf(ret));
        }
        return ret;
    }

    /**
     * 获取公告详情announcement
     */
    @RequestMapping(value = "announcement/{mId}", method = RequestMethod.GET)
    public AnnouncementResponse doGetDetailAnnouncement(@PathVariable long mId) {
        return announcementService.doGetDetailAnnouncement(mId);
    }

    /**
     * 获取公告announcement目录
     */
    @RequestMapping(value = "announcement", method = RequestMethod.GET)
    public LoanResponseNextLong doGetAnnouncement(
            @RequestHeader(value = "User-Agent", required = false, defaultValue = "") String ua,
            @RequestData AnnouncementRequest req) {
        return announcementService.doGetAnnouncement(ua, req);
    }

    /**
     * 获取站内信message
     */
    @RequestMapping(value = "message", method = RequestMethod.GET)
    public MessageResponse doGetMessage(
            @RequestHeader(value = "Authorization", required = false, defaultValue = "") String authorizationToken,
            @RequestData AnnouncementRequest req) {
        return messageService.doGetMessage(authorizationToken, req);
    }

    /**
     * 获取分享
     */
    @RequestMapping(value = "share/{pageId}", method = RequestMethod.GET)
    public ShareResponse doGetShare(@PathVariable long pageId) {
        return shareService.doGetShare(pageId);
    }

    /**
     * 获取详情： 贷款， 银行， 信用卡， 信评
     */
    @RequestMapping(value = "detail/{objId}", method = RequestMethod.GET)
    public DetailResponse doGetDetail(
            @RequestHeader(value = "Authorization", required = false, defaultValue = "") String authorizationToken,
            @PathVariable long objId) {
        return mongoPandoraCreditService.doGetDetail(authorizationToken, objId);
    }

    /**
     * 获取贷款，银行，信评以及信用卡列表
     */
    @RequestMapping(value = "pcredit/{type}", method = RequestMethod.GET)
    public LoanResponseNextLong doGetPandoralist(@PathVariable String type, @RequestData CreditRequest req) {
        return creditService.doGetPandoralist(type, req);
    }

    /**
     * 更新站内信message为已读
     */
    @RequestMapping(value = "message/status", method = RequestMethod.POST, consumes = {MediaType.APPLICATION_JSON_UTF8_VALUE})
    public MessageStatusResponse doUpdateMessageStatus(
            @RequestHeader(value = "Authorization", required = false, defaultValue = "") String authorizationToken,
            @RequestBody MessageStatusRequest req) {
        return messageService.doUpdateMessageStatus(authorizationToken, req);
    }
    @RequestMapping(value = "message/status", method = RequestMethod.POST, consumes = {MediaType.APPLICATION_FORM_URLENCODED_VALUE})
    public MessageStatusResponse doUpdateMessageStatusForm(
            @RequestHeader(value = "Authorization", required = false, defaultValue = "") String authorizationToken,
            @RequestData MessageStatusRequest req) {
        return messageService.doUpdateMessageStatus(authorizationToken, req);
    }

    /**
     * 获取站内信message是否已读
     */
    @RequestMapping(value = "message/status", method = RequestMethod.GET)
    public MessageStatusGetResponse doGetMessageStatus(
            @RequestHeader(value = "Authorization", required = false, defaultValue = "") String authorizationToken) {
        return messageService.doGetMessageStatus(authorizationToken);
    }

    /**
     * 获取pic 图片
     */
    @RequestMapping(value = "pic/{type}", method = RequestMethod.GET)
    public Object doGetBannerWithType(@PathVariable int type, HttpServletResponse response) {
        Object re = mongoBannerService.doGetBannerWithType(type);
        if (re == null) {
            response.setStatus(204);
        }
        return re;
    }

    /**
     * 埋点API
     */
    @RequestMapping(value = "track", method = RequestMethod.POST, consumes = {MediaType.APPLICATION_JSON_UTF8_VALUE})
    public Object doPostTrackInfo(
            @RequestHeader(value = "Authorization", required = false, defaultValue = "") String authorizationToken,
            @RequestBody TrackPostRequest req,
            HttpServletResponse response) {
        Object ret = trackService.doPostTrackInfo(authorizationToken, req);
        if (ret == null) {
            response.setStatus(204);
        }
        return ret;
    }
    @RequestMapping(value = "track", method = RequestMethod.POST, consumes = {MediaType.APPLICATION_FORM_URLENCODED_VALUE})
    public Object doPostTrackInfoForm(
            @RequestHeader(value = "Authorization", required = false, defaultValue = "") String authorizationToken,
            @RequestData TrackPostRequest req,
            HttpServletResponse response) {
        Object ret = trackService.doPostTrackInfo(authorizationToken, req);
        if (ret == null) {
            response.setStatus(204);
        }
        return ret;
    }

    /**
     * 获取公告详情actions
     */
    @RequestMapping(value = "pcredit_actions", method = RequestMethod.GET)
    public PandoraActionsResponse getPandoraActions() {
        return creditService.doGetPandoraActions();
    }

}
